
//  不要在 onLaunch 的时候调用 getCurrentPage()此时 page 还没有生成。 
// getCurrentPage是获取当前页面的实例对象。
var app = getApp();

var util = require('../../../utils/util.js');

Page({

  data: {

    getImageUrl: "",
    SUFFERER_ID: "",

    picture1: "",
    name:"",
    sex: "M",
    pid:"",
    birthdate:"",
    phone:"",
    description:""
  },


  chooseImageTap: function () {
    var that = this;
   
    wx.navigateTo({
      url: '/pages/helper/viewimage?img=' + that.data.getImageUrl + that.data.picture1
    })

  },


  formSubmit: function (e) {
    var that = this;


    var phone = /^1[0-9]{10}$/;
    var resultphone = phone.exec(e.detail.value.telephone);
    if (resultphone == null) {

      util.showRejectToast('不是合法的手机号码!');


      return;

    }


    var atkey = wx.getStorageSync("AccessToken");

    var formdata = {
      AccessToken: atkey,  PHONE: e.detail.value.telephone,
      SICKHISTORY: e.detail.value.description, SUFFERER_ID: that.data.SUFFERER_ID
    }

    /*util.upload_file(app.globalData.editSufferer, that.data.picture1, 'picture1', formdata,
      function (res) {
        util.showSuccessToast(1)
      },
      function () {
        util.showRejectToast('数据保存失败!');
        return;
      }
    )*/

    wx.showLoading({
      title: '数据提交中...'
    })

    // --------- 更新病历信息 ------------------
    wx.request({
      url: app.globalData.editSufferer,
      data: formdata,
      header: {
        'content-type': 'application/json'
      },

      success: function (res) {
        wx.hideLoading();
        util.showSuccessToast(1);
      },
      fail: function () {
        wx.hideToast();
        util.showRejectToast('数据提交失败！');
      }

    })

  },

  loaddata:function(){

    var that = this;
    var atkey = wx.getStorageSync("AccessToken");
    
    wx.showLoading({
      title: '数据加载中...'
    })

    // --------- 得到病信息 ------------------
    wx.request({
      url: app.globalData.getSufferer,
      data: { AccessToken: atkey, SUFFERER_ID: that.data.SUFFERER_ID},
      header: {
        'content-type': 'application/json'
      },

      success: function (res) {

        //var bdate = util.formatDate(res.data.datum.BIRTHDATE);
        that.setData({
          name: res.data.datum.SUFFERERNAME,
          pid: res.data.datum.PID,
          phone: res.data.datum.PHONE,
          picture1: res.data.datum.PIC,
          birthdate: res.data.datum.BIRTHDATE,
          sex: res.data.datum.SEX,
          description: res.data.datum.SICKHISTORY,
          getImageUrl: app.globalData.getSuffererPIC
        });

        wx.hideLoading();

      },
      fail: function () {
        wx.hideLoading();
        util.showRejectToast('数据加载失败！');
      }

    })

  },


  /**
 * 生命周期函数--监听页面加载
 */
  onLoad: function (options) {
    var that = this;

    //接收参数
    this.setData({
      SUFFERER_ID: options.SUFFERER_ID,
      
    

    });

    this.loaddata();

  },

  onShow: function (options) {

    app.userlogin();

  },

})
